package com.huayue.dao;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.RoomRealOrderDetails;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 入住单详情
 */
@Component
public class RoomRealOrderDetailsDao implements BaseDao<RoomRealOrderDetails> {
    @Override
    public int insert(RoomRealOrderDetails entity) {
        // 先将住客信息添加到数据库
        String sqlc = "insert into customer(realname,idnumber,room_id) values(?,?,?)";
        DBHelper.executeUpdate(sqlc, entity.getName(), entity.getIdnumber(), entity.getRoom_id());
        String sql = "insert into room_real_order_details "
            + "(room_real_order_id, customer_id, room_type_id,room_id, price,is_status,days,total_price) "
            + "values ((select MAX(id) from room_real_order),(select MAX(id) from customer),?,?,?,0,?,?)";
        return DBHelper.executeUpdate(sql, entity.getRoom_type_id(), entity.getRoom_id(), entity.getPrice(),
            entity.getDays(), (entity.getPrice().multiply(new BigDecimal(entity.getDays()))));
    }

    @Override
    public int update(RoomRealOrderDetails entity) {
        return 0;
    }

    @Override
    public int delete(RoomRealOrderDetails entity) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        return null;
    }

    @Override
    public Map<String, Object> findOne(RoomRealOrderDetails entity) {
        return null;
    }

    @Override
    public int count(Object... obj) {
        return 0;
    }

    public List<Map<String, Object>> findDetails(String id) {
        String sql = "select  (rrod.price*rrod.days) price,rro.id rroid,rro.ordernumber rronumber, "
            + " rt.id rtid,rt.name rtname,rt.price oneDayPrice,c.realname name,c.idnumber,r.id roomId,r.name rname "
            + " from room_real_order_details rrod "
            + " inner join room_real_order rro on rrod.room_real_order_id = rro.id "
            + " inner join customer c on rrod.customer_id = c.id "
            + " inner join room_type rt on rrod.room_type_id = rt.id inner join room r on rrod.room_id = r.id "
            + " where rrod.is_deleted =0 and rrod.room_real_order_id = ?";
        return DBHelper.executeQuery(sql, id);
    }

    // 修改房间状态
    public int updateRealDetailsStatus9(Integer roomID) {
        String sql =
            "update room_real_order_details set is_status=9 where id=(select rd.id from (select id from room_real_order_details where room_id=? order by id desc limit 0,1)rd)";
        return DBHelper.executeUpdate(sql, roomID);
    }

    // +=
    public int updateRoomRealDetailsDays(Integer id, Integer days, BigDecimal addPrice) {
        String sql = "update room_real_order_details set days=days+?,total_price=total_price+? where id =?";
        return DBHelper.executeUpdate(sql, days, addPrice, id);
    }

}
